package org.wendu.wdoa.emp.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.wendu.wdoa.common.ex.BusinessException;
import org.wendu.wdoa.common.page.PageParam;
import org.wendu.wdoa.common.page.PageUtils;
import org.wendu.wdoa.emp.dao.EmployeeDao;
import org.wendu.wdoa.emp.dto.EmpDto;
import org.wendu.wdoa.emp.dto.EmpQueryDto;
import org.wendu.wdoa.emp.dto.MgrDto;
import org.wendu.wdoa.emp.dto.UserDto;
import org.wendu.wdoa.emp.model.Employee;
import org.wendu.wdoa.emp.service.EmployeeService;


import java.util.Map;

@Service
@Transactional//声明本类中所有方法都是事务性的
@CacheConfig(cacheNames = "emp")
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
EmployeeDao employeeDao;

    @Cacheable(key = "targetClass+'.'+methodName+'('+#p0+')'")
    @Override
    public Map<String,Object> getEmployList(PageParam pageParam) {
       /* QeuryAction<Employee> action = new QeuryAction<Employee>() {
            @Override
            public List<Employee> query() {
                return employeeDao.findEmpList();
            }
        };*/
       /* QeuryAction<Employee> action = ()->employeeDao.findEmpList();
       return PageUtils.getPage(pageParam,action);*/
        return PageUtils.getPage(pageParam,()->employeeDao.findEmpList(pageParam));
    }

    @CacheEvict(allEntries = true)
    @Override
    public void deleteEmpById(String e_id) {
        if (!StringUtils.hasText(e_id)){
            throw new BusinessException("没有提供工号!");
        }
        boolean exists = employeeDao.findExistsId(e_id);
        if (!exists){
            throw new BusinessException("工号"+e_id+"不存在");
        }
        employeeDao.deleteById(e_id);
    }
    @CacheEvict(allEntries = true)
    @Override
    public void deleteEmpById(String[] eids) {
        for (String eid:eids) {
            employeeDao.deleteById(eid);
        }
    }
    @CacheEvict(allEntries = true)
    @Override
    public synchronized void addOneEmp(EmpDto empDto) {
        String newId = employeeDao.findNewId();
        empDto.setE_id(newId);
        employeeDao.insertEmp(empDto);
    }
    @CacheEvict(allEntries = true)
    @Override
    public void updateOneEmp(Employee employee) {
        employeeDao.updateEmp(employee);
    }
    @CacheEvict(allEntries = true)
    @Override
    public void addUser(UserDto empDto) {
        employeeDao.insertEmpUser(empDto);
    }
    @CacheEvict(allEntries = true)
    @Override
    public void delUser(String e_id) {
        employeeDao.deleteEmpUser(e_id);
    }
    @CacheEvict(allEntries = true)
    @Override
    public void addMgr(MgrDto mgrDto) {
        boolean exists = employeeDao.findExistMgr(mgrDto.getD_id());
        String eids = employeeDao.findMgreid(mgrDto.getD_id());
        if (exists){
            throw new BusinessException(eids+"为该部门领导，重设领导需要先撤销已有领导");
        }
        Integer newId = employeeDao.findMgrNewId();
        mgrDto.setMgr_id(newId);
        employeeDao.insertEmpMgr(mgrDto);
    }
    @CacheEvict(allEntries = true)
    @Override
    public void delMgr(String e_id) {
        employeeDao.deleteMgrEmp(e_id);
    }


}
